package com.projetoptymo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ProcheActivity extends Activity implements OnClickListener
{
	BusStop arret;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.prochainbus);
		
		BusStopList bsl = BusStopList.getInstance();
		
		arret = bsl.findNearestStop();
		
		TextView nomArret = (TextView) findViewById(R.id.textnomarret);

		if(arret == null)
		{
			nomArret.setText("Position indisponible");
		}
		else
		{
			nomArret.setText(arret.getNom());
			
			Button btnCarte = (Button) findViewById(R.id.gocarte);
			btnCarte.setOnClickListener(this);
			
			Button btnDestination = (Button) findViewById(R.id.godest);
			btnDestination.setOnClickListener(this);
		}
		
		
		// Gestion du clic sur le bouton clock
        Resources res = getResources();
        StateListDrawable clock = new StateListDrawable();
        clock.addState(new int[] {android.R.attr.state_pressed}, res.getDrawable(R.drawable.cartepressed));
        clock.addState(new int[] {}, res.getDrawable(R.drawable.carte));
        ((Button)findViewById(R.id.gocarte)).setBackgroundDrawable(clock);

		// Gestion du clic sur le bouton clock
        clock = new StateListDrawable();
        clock.addState(new int[] {android.R.attr.state_pressed}, res.getDrawable(R.drawable.clockpressed));
        clock.addState(new int[] {}, res.getDrawable(R.drawable.clock));
        ((Button)findViewById(R.id.godest)).setBackgroundDrawable(clock);
		
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.gocarte:
			Intent i = new Intent(this, LocalMapActivity.class);
			
			Bundle bunble = new Bundle();
			bunble.putString("DisplayPosition", "YES");
			bunble.putString("Latitude", String.valueOf(arret.getPosition().getLatitude()));
			bunble.putString("Longitude", String.valueOf(arret.getPosition().getLongitude()));
	        i.putExtras(bunble);
	        
			startActivity(i);
			break;
			
		case R.id.godest:
			// on reinit les params.
			SharedPreferences pref = getSharedPreferences("settings", MODE_PRIVATE);
			SharedPreferences.Editor editor = pref.edit();
			
			// on reinit les params.
			editor.putBoolean("ChooseDepart", true);
			editor.putString("DepartNom", "");

			editor.commit();
			
			Intent j = new Intent(this, DestinationActivity.class);
			
			Bundle objetbunble = new Bundle();
			objetbunble.putString("DepartNom", arret.getNom());
	        j.putExtras(objetbunble);
			
			startActivity(j);
			break;
		}

	}

}
